# -*- coding: utf8 -*-
import os
import requests

# 打印栏目信息
def print_mv(dicts):
    for k, v in dicts.items():
        print(k, v["title"])
def print_pic(dicts):
    for k, v in dicts.items():
        print(k, v[0])
# 写入文件
def write_file(filename, url):
    data = requests.get(url, headers=header)
    res = data.content
    dir = filename.split("/")[0]
    if os.path.exists(dir) == False:
        os.makedirs(dir)
    with open(filename, mode="wb") as f:
        f.write(res)


# 下载视频
def download_mv(**kwargs):
    while True:
        choose = input("输入编号下载文件,输入Q｜q返回上一层: ")
        if choose.upper() == "Q":
            break
        elif int(choose) not in range(1, len(kwargs) + 1):
            print("请输入正确编号\n")
            continue
        else:
            print("正在下载{}\n".format(kwargs[choose]["title"]))
            filename = "视频/" + kwargs[choose]["title"] + ".mp4"
            write_file(filename, kwargs[choose]["url"])
            global tmpdict
            tmpdict = kwargs
            tmpdict.pop(choose)
            print(kwargs)


# 下载图片
def download_pic(**kwargs):
    while True:
        choose = input("输入编号下载文件,输入Q｜q返回上一层: ")
        if choose.upper() == "Q":
            break
        elif int(choose) not in range(1, len(kwargs) + 1):
            print("请输入正确编号\n")
            continue
        else:
            try:
                print("正在下载{}\n".format(kwargs[choose][0]))
                filename = "图片/" + kwargs[choose][0] + ".jpg"
                write_file(filename, kwargs[choose][1])
                global tmpdict
                tmpdict = kwargs
                tmpdict.pop(choose)
            except KeyError:
                print("文件已下载，请勿重复下载\n")
                continue


header = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"}
image_dict = {
    "1": ("吉他男神", "https://hbimg.huabanimg.com/51d46dc32abe7ac7f83b94c67bb88cacc46869954f478-aP4Q3V"),
    "2": ("漫画美女", "https://hbimg.huabanimg.com/703fdb063bdc37b11033ef794f9b3a7adfa01fd21a6d1-wTFbnO"),
    "3": ("游戏地图", "https://hbimg.huabanimg.com/b438d8c61ed2abf50ca94e00f257ca7a223e3b364b471-xrzoQd"),
    "4": ("alex媳妇", "https://hbimg.huabanimg.com/4edba1ed6a71797f52355aa1de5af961b85bf824cb71-px1nZz"),
}
video_dict = {
    "1": {"title": "东北F4模仿秀",
          'url': "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300f570000bvbmace0gvch7lo53oog"},
    "2": {"title": "卡特扣篮",
          'url': "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f3e0000bv52fpn5t6p007e34q1g"},
    "3": {"title": "罗斯mvp",
          'url': "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f240000buuer5aa4tij4gv6ajqg"},
}
nba_dict = {
    "1": {"title": "威少奇才首秀三双",
          "url": "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0300fc20000bvi413nedtlt5abaa8tg&ratio=720p&line=0"},
    "2": {"title": "塔图姆三分准绝杀",
          "url": "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0d00fb60000bvi0ba63vni5gqts0uag&ratio=720p&line=0"}
}
channel_list = [0, image_dict, video_dict, nba_dict]
while True:
    print("""资源下载，输入对应编号进入专区：
    1----花瓣网图片专区
    2----抖音短视频专区
    3----NBA集锦专区""")
    channel = input("选择专区，输入Q｜q退出选择：\n")
    if channel.upper() == "Q":
        break
    elif int(channel) not in (1, 2, 3):
        print(channel)
        print("编号输入错误，请输入1｜2｜3")
        continue
    else:
        channel = int(channel)
        if int(channel) == 1:
            print_pic(channel_list[channel])
            download_pic(**channel_list[channel])
        else:
            print_mv(channel_list[channel])
            download_mv(**channel_list[channel])
